<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/1 0001
 * Time: 16:00
 */

namespace app\api\controller\v1\cleaner;


use app\api\controller\v1\Cleaner;
use app\api\service\BankCardService;
use think\Db;

class BankCard extends Cleaner
{
    /**
     * 绑定的银行卡列表
     */
    public function index()
    {
        $where = [['cid','eq',$this->cid]];
        $list = Db::name('CleanerBankCard')->where($where)->select();
        $this->success('success',$list);
    }

    /**
     * 绑定银行卡
     */
    public function save()
    {
        $data = $this->_input([
            'real_name' => $this->request->post('real_name'),
            'card_no' => $this->request->post('card_no'),
            'bank_name' => $this->request->post('bank_name'),
            'bank_open_branch_name' => $this->request->post('bank_open_branch_name')
        ],[
            'real_name' => 'require',
            'card_no' => 'require|number',
            'bank_name' => 'require',
            'bank_open_branch_name' => 'require',
        ]);
        //验证银行卡真实性
        $result = BankCardService::checkIdCard($data['card_no']);
        if(!isset($result['result']) || $result['result'] !== 'success')
        {
            $this->error('请输入有效的银行卡号！');
        }
        if($data['bank_name'] != $result['data']['bank_name'])
        {
            $this->error('银行卡号和银行名称不匹配！');
        }
        //插入数据库
        $data['cid'] = $this->cid;
        if(Db::name('CleanerBankCard')->insert($data))
        {
            $this->success('添加银行卡成功！');
        } else {
            $this->error('添加银行卡失败！');
        }
    }
    public function create()
    {
        $this->success('success',BankCardService::$bank_list);
    }
    /**
     * 驗證銀行卡
     * @param $id
     */
    public function read($id)
    {
        $result = BankCardService::checkIdCard($id);
        if(isset($result['result']) && $result['result'] === 'success')
        {
            $this->success('验证成功！',$result['data']);
        } else {
            $this->error('验证失败！');
        }
    }

    /**
     * 解除綁定
     * @param $id
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delete($id)
    {
        $where = [['cid','eq',$this->cid],['id','eq',$id]];
        Db::name('CleanerBankCard')->where($where)->delete();
        $this->success('解除绑定成功！');
    }
}